#!/usr/local/bin/php
<?php
include('helper/url_helper.php');
include('helper/simple_html_dom.php');
include('adodb/adodb.inc.php');
include('config.php');
error_reporting(0);
$db = ADONewConnection($_config['dbdriver']);
$db->Connect($_config['server'],$_config['username'],$_config['password'],$_config['database']);
$db->Execute('SET character_set_results=utf8');
$db->Execute('SET collation_connection=utf8_unicode_ci');
$db->Execute('SET NAMES utf8');
$news = array('crime','economic','entertain','politic','region','social','sport','world');
//$news = array('crime');
foreach($news as $new)
{
print("<br>start ");
$links = get_link($new);
if($links)
{
	foreach ($links as $key => $link)
	{
		$html = file_get_html($link);
		$data['title'] = $html->find('h1[class=each-reader-title]',0)->plaintext;
		$data['detail'] = $html->find('div[id=content_only]',0)->innertext;
		$data['slug'] = clean_url($data['title']);
		$data['created'] = time();
		$db->AutoExecute('sanook_'.$new.'s',$data,'INSERT');
		print ++$key." $new insert <br>";
		unset($html);
		unset($data);
	}
	print count($links)." records updated <br>";
}
else
{
	print($new.' no data updated<br>');
}
print("$new end <br>");
}


/*---------------------------------------function-----------------------------------------------*/
function get_link($name)
{
	global $db;

		$html = file_get_html('http://news.sanook.com/archive/'.$name.'/latest/2011/page/1');
		foreach($html->find('div[id=archive-search-results] ol li h3') as $key => $data)
		{		
			$slug = trim(clean_url($data->plaintext));
			$check = $db->GetOne('select slug from sanook_'.$name.'s where slug = ?',array($slug));
			if(!$check)
			{
						$feed[] =  $data->find('a',0)->href;
			}
			else
			{
				if(isset($feed))
				{
					sort($feed);
					return $feed;
				}
				else
				{
					return false;
				}
			}
		}
		unset($html);
		unset($data);

	if(isset($feed))
	{
		sort($feed);
		return $feed;
	}
	else
	{
		return false;
	}
}
?>